/* eslint-disable */
/*
二，开始倒计时
Countdown(endtime,displayer,str,opts);
//这三个参数是必须要有的
endtime为结束的秒
displayer为显示时钟的元素(只接收原生对象)
str为在displayh里面显示的html字符串，里面包含{d},{h},{m},{s}分别表示天、时、分、秒

opts为一个可选的json对象{start:f(displayer),step:f(displayer,),end:endfunc,ajaxurl:,interval:}
start为开始倒计时执行的函数
step为每秒执行的函数
end为倒计时结束后的函数
ajaxurl为异步获取服务器上的时间
interval为间隔几秒获取一次

ajaxurl和interval只须参一次即可
*/
let Countdown = (function() {
  //关于获取服务器时间的参数
  let url = url || '',
    interval = interval || '',
    //记录秒递增,用以间隔向服务器同步时间戳
    secAdd = 0,
    //倒计时数量
    count = 0,
    //倒计时停止标识
    isBeatStop = true,
    //队列
    list = [],
    //时间戳
    nowTime = 0,
    beatHandle

  //秒增器，本函数是在外部根据addtimer_stop来启动
  let beat = function(opts) {
    //从本地获取时间戳
    nowTime = ((new Date()).getTime() / 1000) >> 0;
    //若有服务器url则从服务器获取时间戳
    /*
    if (url !== '') {
      $.get(url + '&r=' + Math.random(), function (result) {
        nowTime = result;
      });
    }
    */
    beatHandle = setInterval(function () {
      //如果队列不为空
      if (list.length) {
        isBeatStop = true;
        for (let i = 0; i < list.length; i++) {
          if (list[i] != null) {
            //如果仍有
            isBeatStop = false;
          }
        }
        //停止处理
        if (isBeatStop) {
          clearInterval(beatHandle);
          isBeatStop = true;
          list = [];
          count = 0;
          return;
        }
      }
      //计秒器
      secAdd++;
      //本地计算时间戳
      nowTime++;

      //定时从服务器同步时间戳
     /*
     if (secAdd - interval >= 0 && url != '' && interval != '') {
        $.get(url + '&r=' + Math.random(), function(data) {
          nowTime = data;
          secAdd = 0;
        });
      }
      */
      //统一处理所有倒计时器
      for(let i = 0;i < count; i++){
        list[i] !== null && countdown(list[i].endTime,list[i].displayer,list[i].str,list[i].id,list[i].opts);
      }
    }, 1000);
  }

  let remain = function(s, e) {
    let gap = e - s,
      h = (gap / 3600 % 24) >> 0,
      m = (gap / 60 % 60) >> 0;
    s = (gap % 60) >> 0;
    return {
      gap: gap,
      d: ((gap / 3600 / 24) >> 0).toString(),
      h: (h > 9 ? h : '0' + h).toString(),
      m: (m > 9 ? m : '0' + m).toString(),
      s: (s > 9 ? s : '0' + s).toString()
    }
  };

  let countdown = function (endTime, displayer, str, id, opts) {
    let remaining = remain(nowTime, endTime);
    let gap = remaining.gap;
    list[id].remaining = remaining
    if (displayer) {
      displayer.innerHTML = str.replace(/\{\w{1}\}/g, function(str) {
        return remaining[str.slice(1, 2)];
      });
    }
    if (gap < 1) {
      clearInterval(list[id]);
      Object.prototype.toString.call(opts.end) === '[object Function]' && opts.end(list[id]);
      list[id] = null;
      return;
    }
    Object.prototype.toString.call(opts.step) === '[object Function]' && opts.step(list[id]);
  };

  //开时添加一个倒计时
  return function (endTime, displayer, str, opts) {
    opts = opts || {};
    url = opts.url, interval = opts.interval;

    //如果秒钟自增器停止了则运行
    if (isBeatStop) {
      beat();
      isBeatStop = false;
    }

    //一开始调用立即显示倒计时
    let remaining = remain(nowTime, endTime);
    let gap = remaining.gap;

    if (displayer) {
      displayer.innerHTML = str.replace(/\{\w{1}\}/g, function(str) {
        return remaining[str.slice(1, 2)];
      });
    }

    list.push({endTime: endTime, displayer: displayer, str:str, id: count++, opts: opts});
  }
})();

export default Countdown
